package com.llb666.e.shop.domain.service;

import com.llb666.e.shop.api.dto.OrderItemsCommentDto;
import com.llb666.e.shop.domain.entity.bo.MyCommentBo;
import com.llb666.e.shop.domain.repository.ItemsCommentsRepository;
import com.llb666.e.shop.domain.repository.OrderItemsRepository;
import com.llb666.e.shop.domain.repository.OrderStatusRepository;
import com.llb666.e.shop.domain.repository.OrdersRepository;
import com.llb666.e.shop.infracore.web.PagedGridResult;
import lombok.RequiredArgsConstructor;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 斌~
 * @date 2022/9/12 下午6:21
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class MyCommentsService {

    public final OrderItemsRepository orderItemsRepository;
    public final OrdersRepository ordersRepository;
    public final OrderStatusRepository orderStatusRepository;
    public final ItemsCommentsRepository itemsCommentsRepository;
    private final Sid sid;

    public void saveComments(String orderId, String userId, List<OrderItemsCommentDto> commentList) {
        // 1. 保存评价 items_comments
        for (OrderItemsCommentDto oic : commentList) {
            oic.setCommentId(sid.nextShort());
        }
        itemsCommentsRepository.saveComments(userId, commentList);
        // 2. 修改订单表改已评价 orders
        ordersRepository.updateCommentsStatus(orderId);
        // 3. 修改订单状态表的留言时间 order_status
        orderStatusRepository.updateOrderStatus(orderId);
    }

    public PagedGridResult<MyCommentBo> queryMyComments(String userId, Integer page, Integer pageSize) {
        return itemsCommentsRepository.queryMyComments(userId, page, pageSize);
    }
}
